package com.twu.biblioteca.service;

import com.google.common.base.Predicate;
import com.twu.biblioteca.Library.User;
import com.twu.biblioteca.tools.UserFactory;

import java.security.InvalidParameterException;

import static com.google.common.collect.FluentIterable.from;

public class LoginService {


    public User login(final String userId, final String password) {
        User user = null;
        try {
            user = from(UserFactory.initUsers()).firstMatch(new Predicate<User>() {
                @Override
                public boolean apply(User user) {
                    return user.getUserId().equals(userId) && user.getPassword().equals(password);
                }
            }).get();
        } catch (Exception e) {
            throw new InvalidParameterException();
        }
        return user;
    }

}
